package com.nuoniu.sibanyun.controller.cms;

import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.cms.CmsArticleType;
import com.nuoniu.sibanyun.service.cms.ICmsArticleTypeService;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 文章分类管理 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2022-06-17
 */
@RestController
@RequestMapping("/cms/articleType")
@Api(tags = "文章分类管理")
public class CmsArticleTypeController extends BaseController {

    @Autowired
    private ICmsArticleTypeService typeService;

    @ApiOperation("获取列表")
    @GetMapping("getList")
    public HttpResult getList() {
        return HttpResult.ok(typeService.getList());
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody CmsArticleType type) {
        if(type == null || StringUtils.isBlank(type.getName())) {
            return HttpResult.error("名称不能为空");
        }

        if(type.getId() == null) {
            type.setCompanyId(UserThreadLocal.get().getCompanyId());
        }
        typeService.saveOrUpdate(type);
        return HttpResult.ok();
    }

    @ApiOperation("删除")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        typeService.removeById(id);
        return HttpResult.ok();
    }
}
